<?php
/**
 * Created by PhpStorm.
 * Author: QinShuai
 * Email: qinshuai0423@qq.com
 * Date: 2021/9/9
 */

namespace backend\controllers;

use backend\models\ElasticSearch;
use yii\web\Controller;

class TestController extends Controller
{

    public function actionIndex()
    {
        //$res = (new ElasticSearch())->insertRecord();
        //$res = ElasticSearch::get(123);
        //$res = ElasticSearch::find()->all();
        echo "<pre>";
        //$res = \Yii::$app->elasticsearch->createCommand()->getMapping();


        $query = [
            'match'=>[
                'title'=>'elasticsearch study'
            ]
        ];
        $query = [
            'bool'=>[
                'must'=>['match'=>['title'=>'elasticsearch']],
                'must_not'=>['match'=>['title'=>'goods']],
                'should'=>[['match'=>['title'=>'索']]],
                //'filter'=>["range"=>['commment'=>['gt'=>200]]]
            ]
        ];
        $query = [
            "range"=>['commment'=>['gt'=>200]],
        ];
        $res = ElasticSearch::find()
           // ->source(['title'])
            ->query($query)
            ->orderBy('_score DESC')
            //->offset(0)
            ->limit(10)
            ->createCommand()
            ->search();

        $aggs = [
            'terms'=>[
                'field'=>'author'//以 author分组 计算commment平均值
            ],
            'aggs'=>[
                "avg_com"=>[
                    "avg"=>['field'=>'commment']
                ]
            ]
        ];
        $res = ElasticSearch::find()
            ->addAggregate('c_comment',$aggs)
            ->createCommand()
            ->search();

        print_r($res);die;

        $client = \Yii::$app->services->curl;

        $result = $client->get('172.18.42.57:9201/website/blog/_search?size=1');
        var_dump($result);
        die;
    }
}